package com.yang.net.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * @author yds
 * @program net-sokect_stduy
 * @description 作为upd协议通讯的一端
 * @create 2020-09-30 16:13
 **/
public class UdpServer {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(9000); //阻塞式等待消息进入
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
        socket.receive(packet);
        System.out.println("收到来至"+packet.getAddress()+"的消息,消息如下:");
        System.out.println(new String(packet.getData(),0,packet.getLength()));
        socket.close();
    }
}